package com.tjetc.service.impl;

import com.tjetc.domain.Vehicle;
import com.tjetc.mapper.VehicleMapper;
import com.tjetc.service.VehicleService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

public class VehicleServiceImpl implements VehicleService {
    private VehicleMapper vehicleMapper= Db.getMapper(VehicleMapper.class);
    /**
     * 输出所有车辆
     * @return
     */
    @Override
    public Result list() {
        return new Result(200,null,vehicleMapper.list());
    }

    @Override
    public Result add(Vehicle vehicle) {
        //根据车牌号查找是否有车辆已经登记
        Vehicle car =vehicleMapper.selectByLincensePlate(vehicle.getLicensePlate());
        if (car!=null){
            return new Result(500,"已经有该车牌的车,不允许添加",null);
        }
        int row=vehicleMapper.insert(vehicle);
        if (row==1) {
            return new Result(200,"添加成功",null);
        }
        return new Result(500,"添加失败",null);
    }

    @Override
    public Result update(Vehicle vehicle) {
        Vehicle vehicle1=vehicleMapper.selectByPrimaryKey(vehicle.getVehicleId());
        if (vehicle1==null){
            return new Result(500,"没有这个车",null);
        }else {
            int rows=vehicleMapper.update(vehicle);
            if (rows==1){
                return  new Result(200,"更新成功",null);
            }
        }
        return new Result(500,"更新失败",null);
    }

    @Override
    public Result delete(int id) {
        Vehicle vehicle1=vehicleMapper.selectByPrimaryKey(id);
        if (vehicle1==null){
            return new Result(500,"没有这个车",null);
        }else {
            int rows=vehicleMapper.deleteByPrimaryKey(id);
            if (rows==1){
                return  new Result(200,"删除成功",null);
            }
        }
        return new Result(500,"删除失败",null);
    }

    @Override
    public Result listBySome(String model,double min,double max) {
        return new Result(200,null,vehicleMapper.listBySome(model,min,max));

    }
}
